package com.hao.statussheet.fragment;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hao.statussheet.R;
import com.hao.statussheet.handler.ActivitySendService;
import com.hao.statussheet.service.MyMusicService;

/**
 * Created by hao on 16/12/1.
 */
public class TestFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.test_fragment, null);
        view.findViewById(R.id.test_start_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMyServiceRunning(getContext())) {
                    // TODO: 16/12/4 切换数据总的
                    ActivitySendService.getOnMusicOperationListener().onStart();
                } else {
                    Intent intent = new Intent(getContext(), MyMusicService.class);
                    getContext().startService(intent);
                }
            }
        });
        // TODO: 16/12/4 一系列的交互操作均有  ActivitySendService.getOnMusicOperationListener() 处理
        return view;
    }

    private boolean isMyServiceRunning(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.hao.statussheet.service.MyMusicService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }


}

